package com.burgeon.backend.EnterpriseWeChat.rest;

import com.burgeon.backend.EnterpriseWeChat.entity.RFIMVipCount;
import com.burgeon.backend.EnterpriseWeChat.entity.YCOrder;
import com.burgeon.backend.EnterpriseWeChat.entity.YCOrderItem;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindYcOrderItemByDocno extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindYcOrderItemByDocno(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<YCOrderItem> getData(String docno) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(docno), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<YCOrderItem> ycOrderItems = new ArrayList<>();
        if(jsonArray.length()>0){
        for (int i = 0; i<jsonArray.length();i++) {
            YCOrderItem ycOrder = new YCOrderItem();

                ycOrder.setProductValue(jsonArray.getJSONArray(i).getString(0));
                ycOrder.setColor(jsonArray.getJSONArray(i).getString(1));
                ycOrder.setSize(jsonArray.getJSONArray(i).getString(2));
                ycOrder.setProductQty(jsonArray.getJSONArray(i).getString(3));
                ycOrder.setProductId(jsonArray.getJSONArray(i).getString(4));
                ycOrder.setProductName(jsonArray.getJSONArray(i).getString(5));
                ycOrder.setpTotAmtActual(jsonArray.getJSONArray(i).getString(6));

                ycOrderItems.add(ycOrder);
            }
        }


        return ycOrderItems;
    }

    private MultiValueMap Data(String docno) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("'M_PRODUCT_ID;VALUE'");
        columns.add("'M_ATTRIBUTESETINSTANCE_ID;VALUE1'");
        columns.add("'M_ATTRIBUTESETINSTANCE_ID;VALUE2'");
        columns.add("QTY");
        columns.add("'M_PRODUCT_ID'");
        columns.add("'M_PRODUCT_ID;NAME'");
        columns.add("TOT_AMT_ACTUAL");

        params.put("columns", columns);
        params.put("table", "YC_RETAILOUTITEM");//表名
        params.put("count", "true");

        HashMap paramss = new HashMap();
        paramss.put("column", "'YC_RETAILOUT_ID;DOCNO'");
        paramss.put("condition", docno);

        params.put("params", paramss);
//        System.err.println(params);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindYcOrderItemByDocno g = new FindYcOrderItemByDocno("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("YOUT21010200005"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
